CC = gcc
CFLAGS = -Wall -Wextra -pthread -O2

SRC_DIR = src
TEST_DIR = tests/benchmark
BUILD_DIR = build

SRCS = $(SRC_DIR)/locks/spinlock.c \
       $(SRC_DIR)/locks/backoff_spinlock.c \
       $(SRC_DIR)/queue/lockfree_queue.c \
       $(SRC_DIR)/stack/lockfree_stack.c \
       $(SRC_DIR)/hashtable/lockfree_hashtable.c \
       $(SRC_DIR)/ringbuffer/lockfree_ringbuffer.c

OBJS = $(SRCS:%.c=$(BUILD_DIR)/%.o)

TEST_SRCS = $(TEST_DIR)/bench_spinlock.c \
            $(TEST_DIR)/bench_compare.c \
            $(TEST_DIR)/bench_queue.c \
            $(TEST_DIR)/bench_stack.c \
            $(TEST_DIR)/bench_hashtable.c \
            $(TEST_DIR)/bench_ringbuffer.c

TEST_BINS = $(TEST_SRCS:%.c=$(BUILD_DIR)/%)

.PHONY: all clean test benchmark

all: $(BUILD_DIR) $(TEST_BINS)

$(BUILD_DIR):
	mkdir -p $(BUILD_DIR)/$(SRC_DIR)/locks
	mkdir -p $(BUILD_DIR)/$(TEST_DIR)

$(BUILD_DIR)/%.o: %.c
	$(CC) $(CFLAGS) -c $< -o $@

$(BUILD_DIR)/%: %.c $(OBJS)
	$(CC) $(CFLAGS) $^ -o $@

test: $(TEST_BINS)
	$(BUILD_DIR)/$(TEST_DIR)/bench_spinlock

benchmark: $(TEST_BINS)
	$(BUILD_DIR)/$(TEST_DIR)/bench_compare

clean:
	rm -rf $(BUILD_DIR) 